﻿using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
namespace AEFunction.UI
{
    public partial class AttributeTableDlg : Form
    {
        public List<ILayer> layers = new List<ILayer>();
        public AxMapControl axMapControl1;
        private int recordCount = 0;
        private int tapPageIndex = 0;
        private IRgbColor pColor = new RgbColor();
        private List<DataGridView> dataGirdViewList = new List<DataGridView>();

        public void AddNewLayer(ILayer l)
        {
            if (!layers.Contains(l))
                layers.Add(l);
        }

        public void Init(ILayer l, AxMapControl mapControl)
        {
            layers.Add(l);
            axMapControl1 = mapControl;
        }

        public AttributeTableDlg(ILayer l, AxMapControl mapControl)
        {
            InitializeComponent();
            layers.Add(l);
            axMapControl1 = mapControl;
        }

        public AttributeTableDlg()
        {
            InitializeComponent();
        }

        private void AttributeTableDlg_Load(object sender, EventArgs e)
        {
            pColor.Red = 0;
            pColor.Green = 155;
            pColor.Blue = 0;
            tabControl1.Height = panel1.Location.Y - tabControl1.Location.Y;
            AddDataGridView();
            //第一次打开属性表
            int index = layers.Count - 1;
            ShowTabControl(index);
        }

        private void DataGridViewCellClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox1.Text = (e.RowIndex + 1).ToString();
        }

        private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridView dgv = dataGirdViewList[tapPageIndex];
            axMapControl1.Map.ClearSelection();
            IFeatureLayer pFeatureLayer = layers[tapPageIndex] as IFeatureLayer;
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
            IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;
            string tmpID = dgv.Rows[e.RowIndex].Cells[0].Value.ToString();
            int pFeatureID = Convert.ToInt32(tmpID); ;
            IFeature pFeature;
            pFeature = pFeatureClass.GetFeature(pFeatureID);
            pFeatureSelection.Add(pFeature);
            
            pFeatureSelection.SelectionColor = pColor;
            axMapControl1.ActiveView.Extent = pFeature.Extent;
            axMapControl1.ActiveView.Refresh();
        }

        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridView dgv = dataGirdViewList[tapPageIndex];
            axMapControl1.Map.ClearSelection();
            IFeatureLayer pFeatureLayer = layers[tapPageIndex] as IFeatureLayer;
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
            IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;
            string tmpID = dgv.Rows[e.RowIndex].Cells[0].Value.ToString();
            if (tmpID == "")
                return;
            int pFeatureID = Convert.ToInt32(tmpID); ;
            IFeature pFeature;
            pFeature = pFeatureClass.GetFeature(pFeatureID);
            pFeatureSelection.Add(pFeature);
            pFeatureSelection.SelectionColor = pColor;
            axMapControl1.ActiveView.Refresh();
        }

        private void AttributeTableDlg_FormClosing(object sender, FormClosingEventArgs e)
        {
            layers.Clear();
            foreach (DataGridView dgv in dataGirdViewList)
            {
                DataTable dt = (DataTable)dgv.DataSource;
                dt.Clear();
                dgv.Dispose();
            }
            dataGirdViewList.Clear();
            GC.Collect();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar) && e.KeyChar != (char)8)
            {
                e.Handled = true;
                MessageBox.Show("请输入数字");
            }
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            DataGridView dgv = dataGirdViewList[tapPageIndex];
            dgv.CurrentCell = dgv.Rows[Convert.ToInt32(textBox1.Text)].Cells[0];
        }

        public void SwithTabControl(int index)
        {
            AddDataGridView();
            ShowTabControl(index);
        }

        private void AddDataGridView()
        {
            
            ILayer pLastLayer = layers[layers.Count - 1];
            DataTable dt = AEFunction.CreateDataTable(pLastLayer, (pLastLayer as IFeatureLayer).Name);
            DataGridView dataGridView1 = new DataGridView();
            ((System.ComponentModel.ISupportInitialize)(dataGridView1)).BeginInit();
            dataGridView1.Name = "dataGridView1";
            dataGridView1.RowTemplate.Height = 23;
            dataGridView1.ScrollBars = ScrollBars.Both;
            dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            //dataGridView1.Size = new System.Drawing.Size(63, 375);
            //dataGridView1.Location = new System.Drawing.Point(0, 0);
            dataGridView1.CellClick += new DataGridViewCellEventHandler(this.DataGridViewCellClick);
            dataGridView1.RowHeaderMouseDoubleClick += new DataGridViewCellMouseEventHandler(dataGridView1_RowHeaderMouseDoubleClick);
            dataGridView1.RowHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_RowHeaderMouseClick);
            dataGridView1.DataSource = dt;
            dataGridView1.Dock = DockStyle.Fill;
            dataGirdViewList.Add(dataGridView1);
            recordCount = dt.Rows.Count;
        }

        private void ShowTabControl(int index)
        {

            ILayer pLastLayer = layers[index];

            #region 初始化控件
            // 
            // tabPage1
            //
            TabPage tabPage = new TabPage();
            tabPage.Location = new System.Drawing.Point(4, 22);
            tabPage.Name = "tabPage" + (index + 1).ToString();
            tabPage.Padding = new System.Windows.Forms.Padding(3);
            //tabPage.Size = new System.Drawing.Size(663, 375);
            tabPage.TabIndex = 0;
            tabPage.UseVisualStyleBackColor = true;
            this.tabControl1.Controls.Add(tabPage);
            #endregion

            tabPage.Text = (pLastLayer as IFeatureLayer).Name;
            DataGridView dataGridView1 = dataGirdViewList[index];
            tabPage.Controls.Add(dataGridView1);
 
            if (dataGridView1.CurrentRow == null)
                textBox1.Text = "1";
            else
                textBox1.Text = (dataGridView1.CurrentRow.Index + 1).ToString();
            tapPageIndex = index;
            label1.Text = "( " + dataGridView1.SelectedRows.Count.ToString() + " out of " + recordCount.ToString() + " Selected)";
            tabControl1.SelectedTab = tabPage;
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (Control c in tabControl1.SelectedTab.Controls)
            {
                if (c is DataGridView)
                {
                    DataGridView dataGridView1 = c as DataGridView;
                    if (dataGridView1.CurrentRow == null)
                        textBox1.Text = "1";
                    else
                        textBox1.Text = (dataGridView1.CurrentRow.Index + 1).ToString();
                    int count =((DataTable) dataGridView1.DataSource).Rows.Count;
                    label1.Text = "( " + dataGridView1.SelectedRows.Count.ToString() + " out of " + count.ToString() + " Selected)";
                }
            }
        }

        private void AttributeTableDlg_SizeChanged(object sender, EventArgs e)
        {
            tabControl1.Height = panel1.Location.Y - tabControl1.Location.Y;
        }

        public void RemoveTabControl(ILayer l)
        {
            if (layers.Count == 1 && layers[0] == l)
            {
                this.Close();
            }

            else
            {
                for (int i = 0; i < layers.Count; i++)
                {
                    if (layers[i] == l)
                    {
                        DataGridView dgv = dataGirdViewList[i];
                        DataTable dt = (DataTable)dgv.DataSource;
                        dt.Clear();
                        dataGirdViewList.Remove(dgv);
                        layers.Remove(l);
                        dgv.Dispose();
                        tabControl1.Controls.RemoveAt(i);
                    }
                }

                    


            }

        }
    }
}
